from django.contrib import admin
from models import *
from django.db import models
from django.forms import Textarea


class TaskInline(admin.TabularInline):
    model = Task
    extra = 5
    save_as = True
    fields = ('project_code', 'name', 'start_time', 'actual_work_hours', 'note')
    raw_id_fields = ('project_code',)
    
    
class TaskAdmin(admin.ModelAdmin):
    list_display = ('daily_report', 'start_time', 'name', 'actual_work_hours', 'note',)
    search_fields = ['daily_report__note',  'name', 'actual_work_hours', 'note']
    list_filter = ('project_code', )
    save_as = True
    raw_id_fields = ('daily_report', 'project_code',)
    def save_model(self, request, obj, form, change):
        if not change:
            obj.assigned_to = request.user        
        if obj.assigned_to == request.user:
            obj.save()

    def queryset(self, request):
        if not request.user.is_superuser:
            qs = self.model._default_manager.filter(assigned_to=request.user)
        else:
            qs = self.model._default_manager.get_query_set()
        ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
        if ordering:
            qs = qs.order_by(*ordering)
        return qs
    


class ProjectAdmin(admin.ModelAdmin):
    list_select_related = True
    list_display = ('__unicode__', 'owner', 'project_code', 'sub_project',)    
    list_filter = ('owner', 'project_code', 'sub_project',)
    search_fields = ['owner__description',
                     'project_code__description',
                     'sub_project__description',]
    save_on_top = True
    save_as = True
    class Media:
        # Problem in IE with selectboxes
        # Solution javascript
        # http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/        
        js = ("/report_media/js/jquery-1.4.2.min.js",
                "/report_media/js/jquery.selectboxes.js",
              "/report_media/js/ProjectCodeSelection.js")

class DailyReportAdmin(admin.ModelAdmin):
    inlines = [ TaskInline, ]
    exclude = ('assigned_to',)    
    list_select_related = True
    list_display = ('date', 'assigned_to', 'tasks',)    
    list_filter = ('date', 'assigned_to')
    save_on_top = True
    save_as = True    
    formfield_overrides = { models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':10})},
 }
    def save_model(self, request, obj, form, change):
        if not change:
            obj.assigned_to = request.user        
        if obj.assigned_to == request.user:
            obj.save()

    def queryset(self, request):
        if not request.user.is_superuser:
            qs = self.model._default_manager.filter(assigned_to=request.user)
        else:
            qs = self.model._default_manager.get_query_set()
        ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
        if ordering:
            qs = qs.order_by(*ordering)
        return qs

    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for instance in instances:
            instance.assigned_to = request.user
            instance.save()
        formset.save_m2m()

    class Media:
        js = ("/report_media/js/StartTimeCalculation.js",
              "/report_media/js/jquery-1.4.2.min.js",
               )
        css = { "all": ("/report_media/css/style.css",)}


class TaskNotaAdmin(admin.ModelAdmin):
    list_display = ('name', 'status', 'priority', 'scheduled_for' ,'note')
    list_filter = ('status', 'project_code',  'assigned_to', 'priority', 'created', 'completed', 'scheduled_for')
    search_fields = ['name', 'note']
    raw_id_fields = ('project_code', 'daily_report',)


    


admin.site.register(DailyReport, DailyReportAdmin)
admin.site.register(Task, TaskAdmin)
#admin.site.register(TaskNota, TaskNotaAdmin)
admin.site.register(Owner)
admin.site.register(ProjectCode)
admin.site.register(SubProject)
admin.site.register(Project, ProjectAdmin)



